(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.globSystemConfig.updateController', updateController);
	updateController.$inject = ['$rootScope', '$scope', '$timeout', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'systemConfigService'];
	function updateController($rootScope, $scope, $timeout, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, systemConfigService) {
		var ctrl = this, globSystemConfigInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.globSystemConfig = {};
		ctrl.init = function() {
			var params = {
					systemCfg : globSystemConfigInfo.systemCfg
			};
			ctrl.tableData = ctrl.tableData || [];
			systemConfigService.initGlobDynaParm($scope, ctrl, gridService);
			systemConfigService.getGlobServiceCode(ctrl).then(function(serviceCodeOptions){
				authService.update(params).then(function(response) {
					if(response.data == null){
						$scope.go('cda.globSystemConfig.create');
					}else{
						if(globSystemConfigInfo.primaryKey == null){
							$rootScope.initAuditOrNoteParams("","");
						}
					}
					ctrl.globSystemConfig = response.data || globSystemConfigInfo;
					ctrl.globSystemConfig.serviceCode = serviceCodeOptions[0].value;
					ctrl.tableData = ctrl.globSystemConfig.dynamicParams || {};
					ctrl.globSystemConfig.dynamicParams = ctrl.globSystemConfig.dynamicParams || {};
					ctrl.isInitialized = "Y";
					ctrl.getGlobDynaparm();
				});
			});
		};

		 ctrl.getGlobDynaparm = function(){
			 if(ctrl.isInitialized == "Y"){
		         systemConfigService.getGlobDynaparmTwo(ctrl,ctrl.globSystemConfig);
		     }else {
		         ctrl.isInitialized = "N";
		     }
	     };
        
		ctrl.updateSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			authService.checkUpdate(ctrl.globSystemConfig).then(function(response) {
				 if(response.data.existData == true){
					 modalService.openErrorDialogs({
							messages : [ 'js.systemconfig.error.data-manipulation' ]
					 });
				 }else{
					 if (messages.length > 0) {
							modalService.openErrorDialogs({
								'messages' : messages
							});
						}else if (ctrl.globSystemConfig.taskId) {
							angular.forEach(ctrl.globSystemConfig.data, function(serviceEntity) {
								angular.forEach(serviceEntity, function(entity, index) {
									if (entity.dynamicParams[entity.name]) {
										ctrl.globSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
									}
								});
							});
							return authService.updateContinueSave(ctrl.globSystemConfig).then(saveSuccessful, saveFailed);
						}else {
							angular.forEach(ctrl.globSystemConfig.data, function(serviceEntity) {
								angular.forEach(serviceEntity, function(entity, index) {
									if (entity.dynamicParams[entity.name]) {
										ctrl.globSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
									}
								});
							});
							return authService.updateSave(ctrl.globSystemConfig).then(saveSuccessful, saveFailed);
						}
				 }
			});
			
			
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			authService.checkUpdate(ctrl.globSystemConfig).then(function(response) {
				 if(response.data.existData == true){
					 modalService.openErrorDialogs({
							messages : [ 'js.systemconfig.error.data-manipulation' ]
					 });
				 }else{
					if (messages.length > 0) {
						modalService.openErrorDialogs({
							'messages' : messages
						});
					} else if (ctrl.globSystemConfig.taskId) {
						angular.forEach(ctrl.globSystemConfig.data, function(serviceEntity) {
							angular.forEach(serviceEntity, function(entity, index) {
								if (entity.dynamicParams[entity.name]) {
									ctrl.globSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
								}
							});
						});
						return authService.updateContinueSubmit(ctrl.globSystemConfig).then(submitSuccessful, submitFailed);
					}  else {
						angular.forEach(ctrl.globSystemConfig.data, function(serviceEntity) {
							angular.forEach(serviceEntity, function(entity, index) {
								if (entity.dynamicParams[entity.name]) {
									ctrl.globSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
								}
							});
						});
						return authService.updateSubmit(ctrl.globSystemConfig).then(submitSuccessful, submitFailed);
					}
			    }
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				},{
					'name' : 'title.infobar.globSystemConfig.update',
					'state' : 'cda.globSystemConfig.update',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
				modalService.openInfoDialogs({
					'messages' : [ 'title.dialog.successful.save' ]
				});
				if (response.data && response.data.model) {
					ctrl.globSystemConfig = response.data.model;
				}
				if (response.data.task && response.data.task.taskId) {
					authService.saveSuccessfulProcess(response.data.task).then(function(response) {
						var routerInfo = response.data || {}, state = routerInfo.processState;
						delete routerInfo.processState;
						$scope.go(state, routerInfo);
					}, angular.noop);
				}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);